from ApplicationCommand import ApplicationCommand

from pyrfid.asn1.Encoder import Encoder
from pyrfid.asn1.Decoder import Decoder
import time

class EraseMemoryCommand(ApplicationCommand):
    
    def __init__(self, tagID):
        self.cmd = '28FC597E0C'
        self.rsp = '28FC597F0C'
        self.tagID = tagID
        self.listCmdAndData = []
        self.ENCODED_COMMAND = ''
    
    def encode(self):
        encoder = Encoder()
        self.listCmdAndData.append(['06', '05', self.cmd])
        self.listCmdAndData.append(['30', '', ''])
        lenTagIDHex = encoder.lengthEncode(len(self.tagID) / 2)
        self.listCmdAndData.append(['04', lenTagIDHex, self.tagID])
        lenData = len(lenTagIDHex)/2 + len(self.tagID)/2 + 1
        self.listCmdAndData[1][1] = encoder.lengthEncode(lenData)
        self._convertToHexString()
                
        return self.ENCODED_COMMAND
    
    def decode(self, encodedRSP):
        if encodedRSP[4:14] != self.rsp:
            raise 'ResponseTypeError'
        
        
        decoder = Decoder(encodedRSP[16:])
        lenData = decoder.lengthDecode()[0]
        if lenData != len(decoder.data) / 2:
                raise 'LengthError'
        
        decoder.popData(4)
        self.completionCode = decoder.intDecode(1)
        decoder.popData(4)
        self.executionCode = decoder.intDecode(1)
        return {'CompletionCode':self.completionCode, 'ExecutionCode':self.executionCode}

    
if __name__ == '__main__':
    tagId = "E200C0D2B25FD380"

    command = EraseMemoryCommand(tagId)
    print command.encode()